Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Dec 1, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 두 개의 프로그래머스 문제 솔루션 개선

  • 12973번: 스택 기반 짝지어 제거하기 알고리즘 최적화

  • 76502번: 괄호 회전하기 솔루션 리팩토링


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Dec 1, 2025
@github-actions
Copy link

github-actions bot commented Dec 1, 2025

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

Performance Optimization

스택 기반 알고리즘의 시간 복잡도를 O(n)으로 유지하면서, 불필요한 조건 분기 제거 필요.
현재 구현은 중복 코드와 불필요한 continue 문을 포함하고 있어 개선 가능.

function solution2(s) {
  const stack = [];
  for (const c of s) {
    if (stack.length === 0) {
      stack.push(c);
      continue;
    }
    const top = stack[stack.length - 1];
    if (c === top) {
      stack.pop();
    } else {
      stack.push(c);
    }
  }

  return stack.length === 0 ? 1 : 0;
}
Algorithmic Complexity

괄호 유효성 검사 로직에서 중복 조건 및 불필요한 분기 존재.
매칭 로직을 더 간결하고 효율적으로 리팩토링할 수 있으며, 불필요한 반복 로직 최적화 필요.

function solution3(s) {
  let answer = 0;
  const sList = [...s, ...s];
  for (let start = 0; start < s.length; start++) {
    const stack = [];
    let isValid = true;
    for (let offset = 0; offset < s.length; offset++) {
      const c = sList[start + offset];
      if (c === "(" || c === "[" || c === "{") {
        stack.push(c);
      } else {
        const top = stack.pop();
        if (!top) {
          isValid = false;
          break;
        }
        if (top === "(" && c === ")") continue;
        if (top === "[" && c === "]") continue;
        if (top === "{" && c === "}") continue;
        isValid = false;
        break;
      }
    }
    if (isValid && stack.length === 0) answer++;
  }
  return answer;
}

@github-actions
Copy link

github-actions bot commented Dec 1, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
괄호 매칭 로직 개선 및 추상화

괄호 매칭 로직을 별도의 헬퍼 함수로 추출하여 코드의 가독성과 재사용성을 높일 수 있습니다. 중첩된 조건문을 간소화하고 명확한 매칭 함수를 만들어
로직을 개선할 수 있습니다.

Programmers/Level2/76502_괄호_회전하기.js [9-35]

+function isMatchingBracket(open, close) {
+  const bracketPairs = {
+    '(': ')', 
+    '[': ']', 
+    '{': '}'
+  };
+  return bracketPairs[open] === close;
+}
+
 function solution3(s) {
   let answer = 0;
   const sList = [...s, ...s];
   for (let start = 0; start < s.length; start++) {
     const stack = [];
     let isValid = true;
     for (let offset = 0; offset < s.length; offset++) {
       const c = sList[start + offset];
-      if (c === "(" || c === "[" || c === "{") {
+      if ("([{".includes(c)) {
         stack.push(c);
       } else {
         const top = stack.pop();
-        if (!top) {
+        if (!top || !isMatchingBracket(top, c)) {
           isValid = false;
           break;
         }
-        if (top === "(" && c === ")") continue;
-        if (top === "[" && c === "]") continue;
-        if (top === "{" && c === "}") continue;
-        isValid = false;
-        break;
       }
     }
     if (isValid && stack.length === 0) answer++;
   }
   return answer;
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion introduces a clean isMatchingBracket helper function that improves code readability and reduces the complexity of bracket matching logic. It makes the code more maintainable and easier to understand.

Medium
스택 연산 단순화 및 중복 제거

현재 구현은 작동하지만, 불필요한 조건 분기와 중복 코드가 있습니다. 스택의 top을 더 간결하게 확인하고 처리할 수 있습니다.

Programmers/Level2/12973_짝지어_제거하기.js [9-25]

 function solution2(s) {
   const stack = [];
   for (const c of s) {
-    if (stack.length === 0) {
-      stack.push(c);
-      continue;
-    }
-    const top = stack[stack.length - 1];
-    if (c === top) {
+    if (stack.length > 0 && stack[stack.length - 1] === c) {
       stack.pop();
     } else {
       stack.push(c);
     }
   }
 
   return stack.length === 0 ? 1 : 0;
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion simplifies the stack operation by removing an unnecessary if condition and reducing code complexity. The logic remains functionally equivalent to the original implementation.

Medium

@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Dec 2, 2025
@uyeon0 uyeon0 merged commit 340011a into main Dec 2, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

programmers Programmers 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants